<template>
  <el-dialog
    ref="refDlg"
    title="暂停"
    :visible.sync="dialogShow"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="closeDialog"
    width="600px"
    @close="closeDialog"
  >
    <div>
      <el-form
        ref="refStopForm"
        :model="stopForm"
        :rules="ruleStopForm"
        label-width="120px"
      >
        <el-form-item label="暂停时间:" prop="stopTime">
          <el-date-picker
            v-model="stopForm.stopTime"
            type="date"
            :clearable="false"
            size="mini"
            placeholder="选择日期"
            value-format="yyyy-MM-dd HH:mm:ss"
          />
        </el-form-item>
        <el-form-item label="预计恢复时间:" prop="expectRestoreTime">
          <el-date-picker
            v-model="stopForm.expectRestoreTime"
            type="date"
            :picker-options="pickerOptions"
            placeholder="选择日期"
            size="mini"
            :clearable="false"
            value-format="yyyy-MM-dd HH:mm:ss"
          />
        </el-form-item>
        <el-form-item label="暂停原因类型:" prop="stopReasonType">
          <el-select
            v-model="stopForm.stopReasonType"
            style="width:200px"
            placeholder="请选择"
            size="mini"
            clearable
          >
            <el-option
              v-for="item in stopReasonOption"
              :key="item.dictCode"
              :label="item.dictName"
              :value="item.dictCode"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="暂停说明" prop="exceptionDesc">
          <el-input v-model="stopForm.exceptionDesc" maxlength="1000" show-word-limit type="textarea" />
        </el-form-item>
        <el-form-item label="附件">
          <file-uploader
            :data="fileList"
            :limit="20"
            :show-download-temp="false"
            :config-attachment-id="configAttachmentId"
            :file-type="fileTypeName"
          />
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer">
      <el-button size="mini" @click="closeDialog">取消</el-button>
      <el-button size="mini" :loading="stopLoading" type="primary" @click="submitStop">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getExceptionType, getShopAttachment, saveException } from '@/api/common'
import FileUploader from '@/components/importFile'
import { getDictByPatchType } from '@/api/dataDict'
import { postConfigAttachmentTemplate } from '@/api/shopDesign'

export default {
  name: 'StopDesignForm',
  components: { FileUploader },
  props: {
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    current: {
      type: [String, Object],
      default: () => {}
    }
  },
  data() {
    return {
      pickerOptions: {
        disabledDate(time) {
          var date = new Date()
          var y = date.getFullYear()
          var m = date.getMonth() + 1
          var d = date.getDate()
          var nowTime = new Date(`${y}-${m}-${d} 00:00:00`)
          return time.getTime() < nowTime.getTime()
        }
      },
      stopLoading: false,
      configAttachmentId: '',
      fileTypeName: '',
      stopReasonOption: [],
      shopAttachmentDTOS: [],
      fileList: [],
      stopForm: {
        stopTime: '',
        expectRestoreTime: '',
        stopReasonType: '',
        exceptionDesc: ''
      }, // 暂停提交表单
      ruleStopForm: {
        stopTime: [{ required: true, message: '请选择暂停时间', trigger: 'change' }],
        expectRestoreTime: [{ required: true, message: '请选择预计恢复时间', trigger: 'change' }],
        stopReasonType: [{ required: true, message: '请填写暂停原因', trigger: 'change' }],
        exceptionDesc: [{ required: true, message: '请填写暂停说明', trigger: 'change' }]
      }
    }
  },

  computed: {
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  async created() {
    const { STOP_REASON_TYPE } = await getDictByPatchType(['STOP_REASON_TYPE'])
    this.stopReasonOption = STOP_REASON_TYPE
    this.getFileType()// 获取附件
  },

  methods: {
    async getFileType() {
      this.fileLoading = true
      await postConfigAttachmentTemplate().then(res => {
        if (res.success === 1) {
        // 根据modelId获取每个配置id
          const arrTemp = res.data.records.filter(row => row.attachmentType === '设计异常')
          if (arrTemp.length > 0) {
            this.configAttachmentId = arrTemp[0].id
            this.fileTypeName = arrTemp[0].fileTypeName
          }
        }
      })
      this.getFiles()// 获取文件，图片列表
    },
    getFiles() {
      getShopAttachment(this.current.id).then(res => {
        if (res.success === 1) {
        // 根据modelId获取每个配置id
          this.fileList = res.data
          res.data.map(row => {
            if (row.attachmentType === '设计异常') {
              this.fileList.push(row)
            }
          })
          this.fileLoading = false
        }
      }).catch(() => {
        this.fileLoading = false
      })
    },
    closeDialog() {
      this.dialogShow = false
      this.clearForm()
    },
    clearForm(done) {
      this.stopForm = {
        stopTime: '',
        expectRestoreTime: '',
        stopReasonType: '',
        exceptionDesc: ''
      } // 暂停提交表单
      this.fileList = []
      this.stopLoading = false
      this.$refs.refStopForm.resetFields()
    },
    // 提交表单处理
    // 暂停
    async  submitStop() {
      const pass = await this.$refs['refStopForm'].validate()
      if (!pass) return
      const upData = {
        shopAttachmentDTOS: this.fileList,
        constCode: this.current.constCode || '',
        modelId: this.current.id || '',
        exceptionHandleId: '',
        bizCode: this.current.constCode || '',
        bizName: '',
        belongModel: 'SHOP_DESIGN',
        exceptionDesc: this.stopForm.exceptionDesc,
        shopExceptionDetailDTOS: [this.stopForm]
      }
      this.stopLoading = true
      await getExceptionType('暂停').then(res => {
        if (res.success === 1 && res.data.records.length > 0) {
          upData.exceptionHandleId = res.data.records[0].id
        }
      })
      if (!upData.exceptionHandleId) {
        this.$message.warning('未获取到异常配置id')
        return
      }
      this.stopLoading = true
      console.log(upData)
      // return
      await saveException(upData).then(res => {
        if (res.success === 1) {
          this.$message.success('暂停申请提交成功')
          this.$emit('on-success', true)
          console.log('暂停成功后')
          this.closeDialog()
          this.stopLoading = false
        }
      }).catch(() => {
        this.stopLoading = false
      })
      this.closeDialog()
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
